package cn.edu.tju.mining.ldj;

import java.io.IOException;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.edu.tju.mining.Factory;
import cn.edu.tju.mining.vo.query_component.ComponentSingleItem;
import cn.edu.tju.mining.vo.report.Report;

public class DoQueryServlet extends HttpServlet {
	/**
	 * 
	 */
	private static Logger logger;
	
	static {
		logger = Logger.getLogger(DoQueryServlet.class.getName());
	}
	private static final long serialVersionUID = 1L;
	private String reportId;
	

	public String getReportId() {
		return reportId;
	}

	public void setReportId(String reportId) {
		this.reportId = reportId;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		reportId = (String) request.getSession().getAttribute("reportId");
		Report rep = null;
		try {
			rep = ReportPageProcessing.getReport(reportId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		logger.info("output is: ");
		if(rep.getUserInputCompnents().getStartDate() != null){
			logger.info(rep.getUserInputCompnents().getStartDate().getName());
			String accStartDate = request.getParameter(rep.getUserInputCompnents().getStartDate().getName());
			logger.info(accStartDate);
			ComponentSingleItem startDateCmpnt = rep.getUserInputCompnents().getStartDate();
			if("day".equals(startDateCmpnt.getType())){
				rep.getQuery().setDateAcceptStart(
					Factory.utilStr.strToDate(accStartDate, "yyyy-MM-dd"));
			}else if("date_time".equals(startDateCmpnt.getType())){
				rep.getQuery().setDateAcceptStart(
						Factory.utilStr.strToDate(accStartDate, "yyyy-MM-dd HH:mm:ss"));
			}
		}
		if(rep.getUserInputCompnents().getEndDate() != null){
			logger.info(rep.getUserInputCompnents().getEndDate().getName());
			String accEndDate = request.getParameter(rep.getUserInputCompnents().getEndDate().getName());
			logger.info(accEndDate);
			ComponentSingleItem endDateCmpnt = rep.getUserInputCompnents().getEndDate();
			if("day".equals(endDateCmpnt.getType())){
				rep.getQuery().setDateAcceptEnd(
					Factory.utilStr.strToDate(accEndDate, "yyyy-MM-dd"));
			}else if("date_time".equals(endDateCmpnt.getType())){
				rep.getQuery().setDateAcceptEnd(
						Factory.utilStr.strToDate(accEndDate, "yyyy-MM-dd HH:mm:ss"));
			}
		}
		if(rep.getUserInputCompnents().getDepRelated() != null){
			List<Integer> dep = new Vector<Integer>();
			logger.info(rep.getUserInputCompnents().getDepRelated().getName());
			String[] depRelatedValues = request.getParameterValues(rep.getUserInputCompnents().getDepRelated().getName());
			if(depRelatedValues != null){
				for (int i = 0; i < depRelatedValues.length; i++) {
					logger.info(depRelatedValues[i] + " -- ");
					dep.add(Integer.parseInt(depRelatedValues[i]));
				}
				rep.getQuery().setDepRelated(dep);
			}
			
		}
		if(rep.getUserInputCompnents().getDistricts() != null){
			List<Integer> districts = new Vector<Integer>();
			logger.info(rep.getUserInputCompnents().getDistricts().getName());
			String[] districtsValues = request.getParameterValues(rep.getUserInputCompnents().getDistricts().getName());
			if(districtsValues != null){
				for (int i = 0; i < districtsValues.length; i++) {
					logger.info(districtsValues[i] + " -- ");
					districts.add(Integer.parseInt(districtsValues[i]));
				}
				rep.getQuery().setDistricts(districts);
			}
		}
		if(rep.getUserInputCompnents().getMapItem() != null){
			int scale = Integer.parseInt(request.getParameter("scale"));
			int pointSize = Integer.parseInt(request.getParameter("pointSize"));
			int maxPoints = Integer.parseInt(request.getParameter("maxPoints"));
			logger.info("scale =" +scale+ " pointSize=" +pointSize+ " maxPoints=" + maxPoints);
			rep.getVisualConfig().setMaxPoints(maxPoints);
			rep.getVisualConfig().setScale(scale);
			rep.getVisualConfig().setPointSize(pointSize);
		}
		try {
			rep.queryDb();
			logger.debug(rep.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServletException(e);
		}
		String jumpUrl = rep.getChartUrl();
		logger.info(jumpUrl);
		response.sendRedirect(jumpUrl);
//		response.sendRedirect("/dmining/reportPages/queryForm.jsp");
	}
}
